home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / lib / calc / lucas.cal < prev    next >
Text File  |  1995-07-17  |  28KB  |  1,014 lines

  1. /*
  2.  * Copyright (c) 1993 Landon Curt Noll
  3.  * Permission is granted to use, distribute, or modify this source,
  4.  * provided that this copyright notice remains intact.
  5.  *
  6.  * By: Landon Curt Noll
  7.  *     chongo@toad.com  -or-  ...!{pyramid,sun,uunet}!hoptoad!chongo
  8.  *
  9.  *
  10.  * lucas - perform a Lucas primality test on h*2^n-1
  11.  *
  12.  * HISTORICAL NOTE:
  13.  *
  14.  * On 6 August 1989 at 00:53 PDT, the 'Amdahl 6', a team consisting of
  15.  * John Brown, Landon Curt Noll, Bodo Parady, Gene Smith, Joel Smith and
  16.  * Sergio Zarantonello proved the following 65087 digit number to be prime:
  17.  *
  18.  *                  216193
  19.  *            391581 * 2      -1
  20.  *
  21.  * At the time of discovery, this number was the largest known prime.
  22.  * The primality was demonstrated by a program implementing the test
  23.  * found in these routines.  An Amdahl 1200 takes 1987 seconds to test
  24.  * the primality of this number.  A Cray 2 took several hours to
  25.  * confirm this prime.  As of 28 Aug 1993, this prime was the 2nd
  26.  * largest known prime and the largest known non-Mersenne prime.
  27.  *
  28.  * The same team also discovered the following twin prime pair:
  29.  *
  30.  *               11235           11235
  31.  *        1706595 * 2     -1    1706595 * 2     +1
  32.  *
  33.  * As of 28 Aug 1993, these primes was still the largest known twin prime pair.
  34.  *
  35.  * ON GAINING A WORLD RECORD:
  36.  *
  37.  * The routines in calc were designed to be portable, and to work on
  38.  * numbers of 'sane' size.  The Amdahl 6 team used a 'ultra-high speed 
  39.  * multi-precision' package that a machine dependent collection of routines 
  40.  * tuned for a long trace vector processor to work with very large numbers.
  41.  * The heart of the package was a multiplication and square routine that 
  42.  * was based on the PFA Fast Fourier Transform and on Winograd's radix FFTs.
  43.  *
  44.  * Having a fast computer, and a good multi-precision package are
  45.  * critical, but one also needs to know where to look in order to have
  46.  * a good chance at a record.  Knowing what to test is beyond the scope
  47.  * of this routine.  However the following observations are noted:
  48.  *
  49.  *    test numbers of the form h*2^n-1
  50.  *    fix a value of n and vary the value h
  51.  *    n mod 128 == 0
  52.  *    h*2^n-1 is not divisible by any small prime < 2^40
  53.  *    0 < h < 2^39
  54.  *    h*2^n+1 is not divisible by any small prime < 2^40
  55.  *
  56.  * The Mersenne test for '2^n-1' is the fastest known primality test
  57.  * for a given large numbers.  However, it is faster to search for
  58.  * primes of the form 'h*2^n-1'.  When n is around 20000, one can find
  59.  * a prime of the form 'h*2^n-1' in about 1/2 the time.
  60.  *
  61.  * Critical to understanding why 'h*2^n-1' is to observe that primes of
  62.  * the form '2^n-1' seem to bunch around "islands".  Such "islands"
  63.  * seem to be getting fewer and farther in-between, forcing the time
  64.  * for each test to grow longer and longer (worse then O(n^2 log n)).
  65.  * On the other hand, when one tests 'h*2^n-1', fixes 'n' and varies
  66.  * 'h', the time to test each number remains relatively constant.
  67.  *
  68.  * It is clearly a win to eliminate potential test candidates by
  69.  * rejecting numbers that that are divisible by 'small' primes.  We
  70.  * (the "Amdahl 6") rejected all numbers that were divisible by primes
  71.  * less than '2^40'.  We stopped looking for small factors at '2^40'
  72.  * when the rate of candidates being eliminated was slowed down to
  73.  * just a trickle.
  74.  *
  75.  * The 'n mod 128 == 0' restriction allows one to test for divisibility
  76.  * of small primes more quickly.  To test of 'q' is a factor of 'k*2^n-1',
  77.  * one check to see if 'k*2^n mod q' == 1, which is the same a checking
  78.  * if 'h*(2^n mod q) mod q' == 1.  One can compute '2^n mod q' by making
  79.  * use of the following:
  80.  *
  81.  *    if
  82.  *        y = 2^x mod q
  83.  *    then
  84.  *        2^(2x) mod q   == y^2 mod q        0 bit
  85.  *        2^(2x+1) mod q == 2*y^2 mod q        1 bit
  86.  *
  87.  * The choice of which expression depends on the binary pattern of 'n'.
  88.  * Since '1' bits require an extra step (multiply by 2), one should
  89.  * select value of 'n' that contain mostly '0' bits.  The restriction
  90.  * of 'n mod 128 == 0' ensures that the bottom 7 bits of 'n' are 0.
  91.  *
  92.  * By limiting 'h' to '2^39' and eliminating all values divisible by
  93.  * small primes < twice the 'h' limit (2^40), one knows that all
  94.  * remaining candidates are relatively prime.  Thus, when a candidate
  95.  * is proven to be composite (not prime) by the big test, one knows
  96.  * that the factors for that number (whatever they may be) will not
  97.  * be the factors of another candidate.
  98.  *
  99.  * Finally, one should eliminate all values of 'h*2^n-1' where
  100.  * 'h*2^n+1' is divisible by a small primes.  The ideas behind this 
  101.  * point is beyond the scope of this program.
  102.  */
  103.  
  104. global pprod256;    /* product of  "primes up to 256" / "primes up to 46" */
  105. global lib_debug;    /* 1 => print debug statements */
  106.  
  107. /*
  108.  * lucas - lucas primality test on h*2^n-1
  109.  *
  110.  * ABOUT THE TEST:
  111.  *
  112.  * This routine will perform a primality test on h*2^n-1 based on
  113.  * the mathematics of Lucas, Lehmer and Riesel.  One should read
  114.  * the following article:
  115.  *
  116.  * Ref1:
  117.  *    "Lucasian Criteria for the Primality of N=h*2^n-1", by Hans Riesel,
  118.  *    Mathematics of Computation, Vol 23 #108, pp. 869-875, Oct 1969
  119.  *
  120.  * The following book is also useful:
  121.  *
  122.  * Ref2:
  123.  *    "Prime numbers and Computer Methods for Factorization", by Hans Riesel,
  124.  *    Birkhauser, 1985, pp 131-134, 278-285, 438-444
  125.  *
  126.  * A few useful Legendre identities may be found in:
  127.  *
  128.  * Ref3:
  129.  *    "Introduction to Analytic Number Theory", by Tom A. Apostol,
  130.  *    Springer-Verlag, 1984, p 188.
  131.  *
  132.  * This test is performed as follows:    (see Ref1, Theorem 5)
  133.  *
  134.  *    a) generate u(0)         (see the function gen_u0() below)
  135.  *
  136.  *    b) generate u(n-2) according to the rule:
  137.  *
  138.  *        u(i+1) = u(i)^2-2 mod h*2^n-1
  139.  *
  140.  *    c) h*2^n-1 is prime if and only if u(n-2) == 0        Q.E.D. :-)
  141.  *
  142.  * Now the following conditions must be true for the test to work:
  143.  *
  144.  *      n >= 2
  145.  *    h >= 1
  146.  *      h < 2^n
  147.  *    h mod 2 == 1
  148.  *
  149.  * A few misc notes:
  150.  *
  151.  * In order to reduce the number of tests, as attempt to eliminate
  152.  * any number that is divisible by a prime less than 257.  Valid prime
  153.  * candidates less than 257 are declared prime as a special case.
  154.  *
  155.  * The condition 'h mod 2 == 1' is not a problem.  Say one is testing
  156.  * 'j*2^m-1', where j is even.  If we note that:
  157.  *
  158.  *      j mod 2^x == 0 for x>0   implies   j*2^m-1 == ((j/2^x)*2^(m+x))-1,
  159.  *
  160.  * then we can let h=j/2^x and n=m+x and test 'h*2^n-1' which is the value.
  161.  * We need only consider odd values of h because we can rewrite our numbers
  162.  * do make this so.
  163.  *
  164.  * input:
  165.  *    h    the h as in h*2^n-1
  166.  *    n    the n as in h*2^n-1
  167.  *
  168.  * returns:
  169.  *    1 => h*2^n-1 is prime
  170.  *    0 => h*2^n-1 is not prime
  171.  *     -1 => a test could not be formed, or h >= 2^n, h <= 0, n <= 0
  172.  */
  173. define
  174. lucas(h, n)
  175. {
  176.     local testval;        /* h*2^n-1 */
  177.     local shiftdown;    /* the power of 2 that divides h */
  178.     local u;        /* the u(i) sequence value */
  179.     local v1;        /* the v(1) generator of u(0) */
  180.     local i;        /* u sequence cycle number */
  181.     local oldh;        /* pre-reduced h */
  182.     local oldn;        /* pre-reduced n */
  183.     local bits;        /* highbit of h*2^n-1 */
  184.  
  185.     /*
  186.      * check arg types
  187.      */
  188.     if (!isint(h)) {
  189.         ldebug("lucas", "h is non-int");
  190.         quit "FATAL: bad args: h must be an integer";
  191.     }
  192.     if (!isint(n)) {
  193.         ldebug("lucas", "n is non-int");
  194.         quit "FATAL: bad args: n must be an integer";
  195.     }
  196.  
  197.     /*
  198.      * reduce h if even
  199.      *
  200.      * we will force h to be odd by moving powers of two over to 2^n
  201.      */
  202.     oldh = h;
  203.     oldn = n;
  204.     shiftdown = fcnt(h,2);  /* h % 2^shiftdown == 0, max shiftdown */
  205.     if (shiftdown > 0) {
  206.         h >>= shiftdown;
  207.         n += shiftdown;
  208.     }
  209.  
  210.     /*
  211.      * enforce the 0 < h < 2^n rule
  212.      */
  213.     if (h <= 0 || n <= 0) {
  214.         print "ERROR: reduced args violate the rule: 0 < h < 2^n";
  215.         print "    ERROR: h=":oldh, "n=":oldn, "reduced h=":h, "n=":n;
  216.         ldebug("lucas", "unknown: h <= 0 || n <= 0");
  217.         return -1;
  218.     }
  219.     if (highbit(h) >= n) {
  220.         print "ERROR: reduced args violate the rule: h < 2^n";
  221.         print "    ERROR: h=":oldh, "n=":oldn, "reduced h=":h, "n=":n;
  222.         ldebug("lucas", "unknown: highbit(h) >= n");
  223.         return -1;
  224.     }
  225.  
  226.     /*
  227.      * catch the degenerate case of h*2^n-1 == 1
  228.      */
  229.     if (h == 1 && n == 1) {
  230.         ldebug("lucas", "not prime: h == 1 && n == 1");
  231.         return 0;    /* 1*2^1-1 == 1 is not prime */
  232.     }
  233.  
  234.     /*
  235.      * catch the degenerate case of n==2
  236.      *
  237.      * n==2 and 0<h<2^n  ==>  0<h<4
  238.      *
  239.      * Since h is now odd  ==>  h==1 or h==3
  240.      */
  241.     if (h == 1 && n == 2) {
  242.         ldebug("lucas", "prime: h == 1 && n == 2");
  243.         return 1;    /* 1*2^2-1 == 3 is prime */
  244.     }
  245.     if (h == 3 && n == 2) {
  246.         ldebug("lucas", "prime: h == 3 && n == 2");
  247.         return 1;    /* 3*2^2-1 == 11 is prime */
  248.     }
  249.  
  250.     /*
  251.      * catch small primes < 257
  252.      *
  253.      * We check for only a few primes because the other primes < 257
  254.      * violate the checks above.
  255.      */
  256.     if (h == 1) {
  257.         if (n == 3 || n == 5 || n == 7) {
  258.             ldebug("lucas", "prime: 3, 7, 31, 127 are prime");
  259.             return 1;    /* 3, 7, 31, 127 are prime */
  260.         }
  261.     }
  262.     if (h == 3) {
  263.         if (n == 2 || n == 3 || n == 4 || n == 6) {
  264.             ldebug("lucas", "prime: 11, 23, 47, 191 are prime");
  265.             return 1;    /* 11, 23, 47, 191 are prime */
  266.         }
  267.     }
  268.     if (h == 5 && n == 4) {
  269.         ldebug("lucas", "prime: 79 is prime");
  270.         return 1;        /* 79 is prime */
  271.     }
  272.     if (h == 7 && n == 5) {
  273.         ldebug("lucas", "prime: 223 is prime");
  274.         return 1;        /* 223 is prime */
  275.     }
  276.     if (h == 15 && n == 4) {
  277.         ldebug("lucas", "prime: 239 is prime");
  278.         return 1;        /* 239 is prime */
  279.     }
  280.  
  281.     /*
  282.      * Avoid any numbers divisible by small primes
  283.      */
  284.     /*
  285.      * check for 3 <= prime factors < 29
  286.      * pfact(28)/2 = 111546435
  287.      */
  288.     testval = h*2^n - 1;
  289.     if (gcd(testval, 111546435) > 1) {
  290.         /* a small 3 <= prime < 29 divides h*2^n-1 */
  291.         ldebug("lucas","not-prime: 3<=prime<29 divides h*2^n-1");
  292.         return 0;
  293.     }
  294.     /*
  295.      * check for 29 <= prime factors < 47
  296.      * pfact(46)/pfact(28) = 5864229
  297.      */
  298.     if (gcd(testval, 58642669) > 1) {
  299.         /* a small 29 <= prime < 47 divides h*2^n-1 */
  300.         ldebug("lucas","not-prime: 29<=prime<47 divides h*2^n-1");
  301.         return 0;
  302.     }
  303.     /*
  304.      * check for prime 47 <= factors < 257, if h*2^n-1 is large
  305.      * 2^282 > pfact(256)/pfact(46) > 2^281
  306.      */
  307.     bits = highbit(testval);
  308.     if (bits >= 281) {
  309.         if (pprod256 <= 0) {
  310.             pprod256 = pfact(256)/pfact(46);
  311.         }
  312.         if (gcd(testval, pprod256) > 1) {
  313.             /* a small 47 <= prime < 257 divides h*2^n-1 */
  314.             ldebug("lucas",\
  315.                 "not-prime: 47<=prime<257 divides h*2^n-1");
  316.             return 0;
  317.         }
  318.     }
  319.  
  320.     /*
  321.      * try to compute u(0)
  322.      *
  323.      * We will use gen_v1() to give us a v(1) using the values
  324.      * of 'h' and 'n'.  We will then use gen_u0() to convert
  325.      * the v(1) into u(0).
  326.      *
  327.      * If gen_v1() returns a negative value, then we failed to
  328.      * generate a test for h*2^n-1.  This is because h mod 3 == 0
  329.      * is hard to do, and in rare cases, exceed the tables found
  330.      * in this program.  We will generate an message and assume
  331.      * the number is not prime, even though if we had a larger
  332.      * table, we might have been able to show that it is prime.
  333.      */
  334.     v1 = gen_v1(h, n, testval);
  335.     if (v1 < 0) {
  336.         /* failure to test number */
  337.         print "unable to compute v(1) for", h : "*2^" : n : "-1";
  338.         ldebug("lucas", "unknown: no v(1)");
  339.         return -1;
  340.     }
  341.     u = gen_u0(h, n, testval, v1);
  342.  
  343.     /*
  344.      * compute u(n-2)
  345.      */
  346.     for (i=3; i <= n; ++i) {
  347.         u = (u^2 - 2) % testval;
  348.     }
  349.  
  350.     /*
  351.      * return 1 if prime, 0 is not prime
  352.      */
  353.     if (u == 0) {
  354.         ldebug("lucas", "prime: end of test");
  355.         return 1;
  356.     } else {
  357.         ldebug("lucas", "not-prime: end of test");
  358.         return 0;
  359.     }
  360. }
  361.  
  362. /*
  363.  * gen_u0 - determine the initial Lucas sequence for h*2^n-1
  364.  *
  365.  * According to Ref1, Theorem 5:
  366.  *
  367.  *    u(0) = alpha^h + alpha^(-h)
  368.  *
  369.  * Now:
  370.  *
  371.  *    v(x) = alpha^x + alpha^(-x)    (Ref1, bottom of page 872)
  372.  *
  373.  * Therefore:
  374.  *
  375.  *    u(0) = v(h)
  376.  *
  377.  * We calculate v(h) as follows:    (Ref1, top of page 873)
  378.  *
  379.  *    v(0) = alpha^0 + alpha^(-0) = 2
  380.  *    v(1) = alpha^1 + alpha^(-1) = gen_v1(h,n)
  381.  *    v(n+2) = v(1)*v(n+1) - v(n)
  382.  *
  383.  * This function does not concern itself with the value of 'alpha'.
  384.  * The gen_v1() function is used to compute v(1), and identity
  385.  * functions take it from there.
  386.  *
  387.  * It can be shown that the following are true:
  388.  *
  389.  *    v(2*n) = v(n)^2 - 2
  390.  *    v(2*n+1) = v(n+1)*v(n) - v(1)
  391.  *
  392.  * To prevent v(x) from growing too large, one may replace v(x) with
  393.  * `v(x) mod h*2^n-1' at any time.
  394.  *
  395.  * See the function gen_v1() for details on the value of v(1).
  396.  *
  397.  * input:
  398.  *    h    - h as in h*2^n-1    (h mod 2 != 0)
  399.  *    n    - n as in h*2^n-1
  400.  *    testval    - h*2^n-1
  401.  *    v1    - gen_v1(h,n)        (see function below)
  402.  *
  403.  * returns:
  404.  *    u(0)    - initial value for Lucas test on h*2^n-1
  405.  *    -1    - failed to generate u(0)
  406.  */
  407. define
  408. gen_u0(h, n, testval, v1)
  409. {
  410.     local shiftdown;    /* the power of 2 that divides h */
  411.     local r;        /* low value: v(n) */
  412.     local s;        /* high value: v(n+1) */
  413.     local hbits;        /* highest bit set in h */
  414.     local i;
  415.  
  416.     /*
  417.      * check arg types
  418.      */
  419.     if (!isint(h)) {
  420.         quit "bad args: h must be an integer";
  421.     }
  422.     if (!isint(n)) {
  423.         quit "bad args: n must be an integer";
  424.     }
  425.     if (!isint(testval)) {
  426.         quit "bad args: testval must be an integer";
  427.     }
  428.     if (!isint(v1)) {
  429.         quit "bad args: v1 must be an integer";
  430.     }
  431.     if (testval <= 0) {
  432.         quit "bogus arg: testval is <= 0";
  433.     }
  434.     if (v1 <= 0) {
  435.         quit "bogus arg: v1 is <= 0";
  436.     }
  437.  
  438.     /*
  439.      * enforce the h mod rules
  440.      */
  441.     if (h%2 == 0) {
  442.         quit "h must not be even";
  443.     }
  444.  
  445.     /*
  446.      * enforce the h > 0 and n >= 2 rules
  447.      */
  448.     if (h <= 0 || n < 1) {
  449.         quit "reduced args violate the rule: 0 < h < 2^n";
  450.     }
  451.     hbits = highbit(h);
  452.     if (hbits >= n) {
  453.         quit "reduced args violate the rule: 0 < h < 2^n";
  454.     }
  455.  
  456.     /*
  457.      * build up u2 based on the reversed bits of h
  458.      */
  459.     /* setup for bit loop */
  460.     r = v1;
  461.     s = (r^2 - 2);
  462.  
  463.     /*
  464.      * deal with small h as a special case
  465.      *
  466.      * The h value is odd > 0, and it needs to be
  467.      * at least 2 bits long for the loop below to work.
  468.      */
  469.     if (h == 1) {
  470.         ldebug("gen_u0", "quick h == 1 case");
  471.         return r%testval;
  472.     }
  473.  
  474.     /* cycle from second highest bit to second lowest bit of h */
  475.     for (i=hbits-1; i > 0; --i) {
  476.  
  477.         /* bit(i) is 1 */
  478.         if (isset(h,i)) {
  479.  
  480.             /* compute v(2n+1) = v(r+1)*v(r)-v1 */
  481.             r = (r*s - v1) % testval;
  482.  
  483.             /* compute v(2n+2) = v(r+1)^2-2 */
  484.             s = (s^2 - 2) % testval;
  485.  
  486.         /* bit(i) is 0 */
  487.         } else {
  488.  
  489.             /* compute v(2n+1) = v(r+1)*v(r)-v1 */
  490.             s = (r*s - v1) % testval;
  491.  
  492.             /* compute v(2n) = v(r)^-2 */
  493.             r = (r^2 - 2) % testval;
  494.         }
  495.     }
  496.  
  497.     /* we know that h is odd, so the final bit(0) is 1 */
  498.     r = (r*s - v1) % testval;
  499.  
  500.     /* compute the final u2 return value */
  501.     return r;
  502. }
  503.  
  504. /*
  505.  * Trial tables used by gen_v1()
  506.  *
  507.  * When h mod 3 == 0, one needs particular values of D, a and b (see gen_v1
  508.  * documentation) in order to find a value of v(1).
  509.  *
  510.  * This table defines 'quickmax' possible tests to be taken in ascending
  511.  * order.  The v1_qval[x] refers to a v(1) value from Ref1, Table 1.  A
  512.  * related D value is found in d_qval[x].  All D values expect d_qval[1]
  513.  * are also taken from Ref1, Table 1.  The case of D == 21 as listed in
  514.  * Ref1, Table 1 can be changed to D == 7 for the sake of the test because
  515.  * of {note 6}.
  516.  *
  517.  * It should be noted that the D values all satisfy the selection values
  518.  * as outlined in the gen_v1() function comments.  That is:
  519.  *
  520.  *       D == P*(2^f)*(3^g)
  521.  *
  522.  * where f == 0 and g == 0, P == D.  So we simply need to check that
  523.  * one of the following two cases are true:
  524.  *
  525.  *       P mod 4 ==  1  and  J(h*2^n-1 mod P, P) == -1
  526.  *       P mod 4 == -1  and  J(h*2^n-1 mod P, P) ==  1
  527.  *
  528.  * In all cases, the value of r is:
  529.  *
  530.  *       r == Q*(2^j)*(3^k)*(z^2)
  531.  *
  532.  * where Q == 1.  No further processing is needed to compute v(1) when r 
  533.  * is of this form.  
  534.  */
  535. quickmax = 8;
  536. mat d_qval[quickmax];
  537. mat v1_qval[quickmax];
  538. d_qval[0] = 5;        v1_qval[0] = 3;        /* a=1   b=1  r=4  */
  539. d_qval[1] = 7;        v1_qval[1] = 5;        /* a=3   b=1  r=12  D=21 */
  540. d_qval[2] = 13;        v1_qval[2] = 11;    /* a=3   b=1  r=4  */
  541. d_qval[3] = 11;        v1_qval[3] = 20;    /* a=3   b=1  r=2  */
  542. d_qval[4] = 29;        v1_qval[4] = 27;    /* a=5   b=1  r=4  */
  543. d_qval[5] = 53;        v1_qval[5] = 51;    /* a=53  b=1  r=4  */
  544. d_qval[6] = 17;        v1_qval[6] = 66;    /* a=17  b=1  r=1  */
  545. d_qval[7] = 19;        v1_qval[7] = 74;    /* a=38  b=1  r=2  */
  546.  
  547. /*
  548.  * gen_v1 - compute the v(1) for a given h*2^n-1 if we can
  549.  *
  550.  * This function assumes:
  551.  *
  552.  *    n > 2            (n==2 has already been eliminated)
  553.  *    h mod 2 == 1
  554.  *    h < 2^n
  555.  *    h*2^n-1 mod 3 != 0    (h*2^n-1 has no small factors, such as 3)
  556.  *
  557.  * The generation of v(1) depends on the value of h.  There are two cases
  558.  * to consider, h mod 3 != 0, and h mod 3 == 0.
  559.  *
  560.  ***
  561.  *
  562.  * Case 1:    (h mod 3 != 0)
  563.  *
  564.  * This case is easy and always finds v(1).
  565.  *
  566.  * In Ref1, page 869, one finds that if:    (or see Ref2, page 131-132)
  567.  *
  568.  *    h mod 6 == +/-1
  569.  *    h*2^n-1 mod 3 != 0
  570.  *
  571.  * which translates, gives the functions assumptions, into the condition:
  572.  *
  573.  *    h mod 3 != 0
  574.  *
  575.  * If this case condition is true, then:
  576.  *
  577.  *    u(0) = (2+sqrt(3))^h + (2-sqrt(3))^h        (see Ref1, page 869)
  578.  *         = (2+sqrt(3))^h + (2+sqrt(3))^(-h)
  579.  *
  580.  * and since Ref1, Theorem 5 states:
  581.  *
  582.  *    u(0) = alpha^h + alpha^(-h)
  583.  *    r = abs(2^2 - 1^2*3) = 1
  584.  *
  585.  * and the bottom of Ref1, page 872 states:
  586.  *
  587.  *    v(x) = alpha^x + alpha^(-x)
  588.  *
  589.  * If we let:
  590.  *
  591.  *    alpha = (2+sqrt(3))
  592.  *
  593.  * then
  594.  *
  595.  *    u(0) = v(h)
  596.  *
  597.  * so we simply return
  598.  *
  599.  *    v(1) = alpha^1 + alpha^(-1)
  600.  *         = (2+sqrt(3)) + (2-sqrt(3))
  601.  *         = 4
  602.  *
  603.  ***
  604.  *
  605.  * Case 2:    (h mod 3 == 0)
  606.  *
  607.  * This case is not so easy and finds v(1) in most all cases.  In this
  608.  * version of this program, we will simply return -1 (failure) if we
  609.  * hit one of the cases that fall thru the cracks.  This does not happen
  610.  * often, so this is not too bad.
  611.  *
  612.  * Ref1, Theorem 5 contains the following definitions:
  613.  *
  614.  *        r = abs(a^2 - b^2*D)
  615.  *        alpha = (a + b*sqrt(D))^2/r
  616.  *
  617.  * where D is 'square free', and 'alpha = epsilon^s' (for some s>0) are units
  618.  * in the quadratic field K(sqrt(D)).
  619.  *
  620.  * One can find possible values for a, b and D in Ref1, Table 1 (page 872).
  621.  * (see the file lucas_tbl.cal)
  622.  *
  623.  * Now Ref1, Theorem 5 states that if:
  624.  *
  625.  *    L(D, h*2^n-1) = -1                [condition 1]
  626.  *    L(r, h*2^n-1) * (a^2 - b^2*D)/r = -1        [condition 2]
  627.  *
  628.  * where L(x,y) is the Legendre symbol (see below), then:
  629.  *
  630.  *    u(0) = alpha^h + alpha^(-h)
  631.  *
  632.  * The bottom of Ref1, page 872 states:
  633.  *
  634.  *    v(x) = alpha^x + alpha^(-x)
  635.  *
  636.  * thus since:
  637.  *
  638.  *    u(0) = v(h)
  639.  *
  640.  * so we want to return:
  641.  *
  642.  *    v(1) = alpha^1 + alpha^(-1)
  643.  *
  644.  * Therefore we need to take a given (D,a,b), determine if the two conditions
  645.  * are true, and return the related v(1).
  646.  *
  647.  * Before we address the two conditions, we need some background information
  648.  * on two symbols, Legendre and Jacobi.  In Ref 2, pp 278, 284-285, we find
  649.  * the following definitions of J(a,p) and L(a,n):
  650.  *
  651.  * The Legendre symbol L(a,p) takes the value:
  652.  *
  653.  *    L(a,p) == 1    => a is a quadratic residue of p
  654.  *    L(a,p) == -1    => a is NOT a quadratic residue of p
  655.  *
  656.  * when
  657.  *
  658.  *    p is prime
  659.  *    p mod 2 == 1
  660.  *    gcd(a,p) == 1
  661.  *
  662.  * The value x is a quadratic residue of y if there exists some integer z
  663.  * such that:
  664.  *
  665.  *    z^2 mod y == x
  666.  *
  667.  * The Jacobi symbol J(x,y) takes the value:
  668.  *
  669.  *    J(x,y) == 1    => y is not prime, or x is a quadratic residue of y
  670.  *    J(x,y) == -1    => x is NOT a quadratic residue of y
  671.  *
  672.  * when
  673.  *
  674.  *    y mod 2 == 1
  675.  *    gcd(x,y) == 1
  676.  *
  677.  * In the following comments on Legendre and Jacobi identities, we shall
  678.  * assume that the arguments to the symbolic are valid over the symbol
  679.  * definitions as stated above.
  680.  *
  681.  * In Ref2, pp 280-284, we find that:
  682.  *
  683.  *    L(a,p)*L(b,p) == L(a*b,p)                {A3.5}
  684.  *    J(x,y)*J(z,y) == J(x*z,y)                {A3.14}
  685.  *    L(a,p) == L(p,a) * (-1)^((a-1)*(p-1)/4)            {A3.8}
  686.  *    J(x,y) == J(y,x) * (-1)^((x-1)*(y-1)/4)            {A3.17}
  687.  *
  688.  * The equality L(a,p) == J(a,p) when:                {note 0}
  689.  *
  690.  *    p is prime
  691.  *    p mod 2 == 1
  692.  *    gcd(a,p) == 1
  693.  *
  694.  * It can be shown that (see Ref3):
  695.  *
  696.  *    L(a,p) == L(a mod p, p)                    {note 1}
  697.  *    L(z^2, p) == 1                        {note 2}
  698.  *
  699.  * From Ref2, table 32:
  700.  *
  701.  *    p mod 8 == +/-1   implies  L(2,p) == 1            {note 3}
  702.  *    p mod 12 == +/-1  implies  L(3,p) == 1            {note 4}
  703.  *
  704.  * Since h*2^n-1 mod 8 == -1, for n>2, note 3 implies:
  705.  *
  706.  *    L(2, h*2^n-1) == 1            (n>2)        {note 5}
  707.  *
  708.  * Since h=3*A, h*2^n-1 mod 12 == -1, for A>0, note 4 implies:
  709.  *
  710.  *    L(3, h*2^n-1) == 1                    {note 6}
  711.  *
  712.  * By use of {A3.5}, {note 2}, {note 5} and {note 6}, one can show:
  713.  *
  714.  *    L((2^g)*(3^l)*(z^2), h*2^n-1) == 1  (g>=0,l>=0,z>0,n>2)    {note 7}
  715.  *
  716.  * Returning to the testing of conditions, take condition 1:
  717.  *
  718.  *    L(D, h*2^n-1) == -1            [condition 1]
  719.  *
  720.  * In order for J(D, h*2^n-1) to be defined, we must ensure that D
  721.  * is not a factor of h*2^n-1.  This is done by pre-screening h*2^n-1 to
  722.  * not have small factors and selecting D less than that factor check limit.
  723.  *
  724.  * By use of {note 7}, we can show that when we choose D to be:
  725.  *
  726.  *    D is square free
  727.  *    D = P*(2^f)*(3^g)            (P is prime>2)
  728.  *
  729.  * The square free condition implies f = 0 or 1, g = 0 or 1.  If f and g
  730.  * are both 1, P must be a prime > 3.
  731.  *
  732.  * So given such a D value:
  733.  *
  734.  *    L(D, h*2^n-1) == L(P*(2^g)*(3^l), h*2^n-1)
  735.  *              == L(P, h*2^n-1) * L((2^g)*(3^l), h*2^n-1)       {A3.5}
  736.  *              == L(P, h*2^n-1) * 1                   {note 7}
  737.  *              == L(h*2^n-1, P)*(-1)^((h*2^n-2)*(P-1)/4)           {A3.8}
  738.  *              == L(h*2^n-1 mod P, P)*(-1)^((h*2^n-2)*(P-1)/4)  {note 1}
  739.  *              == J(h*2^n-1 mod P, P)*(-1)^((h*2^n-2)*(P-1)/4)  {note 0}
  740.  *
  741.  * When does J(h*2^n-1 mod P, P)*(-1)^((h*2^n-2)*(P-1)/4) take the value of -1,
  742.  * thus satisfy [condition 1]?  The answer depends on P.  Now P is a prime>2,
  743.  * thus P mod 4 == 1 or -1.
  744.  *
  745.  * Take P mod 4 == 1:
  746.  *
  747.  *    P mod 4 == 1  implies  (-1)^((h*2^n-2)*(P-1)/4) == 1
  748.  *
  749.  * Thus:
  750.  *
  751.  *    L(D, h*2^n-1) == L(h*2^n-1 mod P, P) * (-1)^((h*2^n-2)*(P-1)/4)
  752.  *              == L(h*2^n-1 mod P, P)
  753.  *              == J(h*2^n-1 mod P, P)
  754.  *
  755.  * Take P mod 4 == -1:
  756.  *
  757.  *    P mod 4 == -1  implies  (-1)^((h*2^n-2)*(P-1)/4) == -1
  758.  *
  759.  * Thus:
  760.  *
  761.  *    L(D, h*2^n-1) == L(h*2^n-1 mod P, P) * (-1)^((h*2^n-2)*(P-1)/4)
  762.  *              == L(h*2^n-1 mod P, P) * -1
  763.  *              == -J(h*2^n-1 mod P, P)
  764.  *
  765.  * Therefore [condition 1] is met if, and only if, one of the following
  766.  * to cases are true:
  767.  *
  768.  *    P mod 4 ==  1  and  J(h*2^n-1 mod P, P) == -1
  769.  *    P mod 4 == -1  and  J(h*2^n-1 mod P, P) ==  1
  770.  *
  771.  * Now consider [condition 2]:
  772.  *
  773.  *    L(r, h*2^n-1) * (a^2 - b^2*D)/r == -1    [condition 2]
  774.  *
  775.  * We select only a, b, r and D values where:
  776.  *
  777.  *    (a^2 - b^2*D)/r == -1
  778.  *
  779.  * Therefore in order for [condition 2] to be met, we must show that:
  780.  *
  781.  *    L(r, h*2^n-1) == 1
  782.  *
  783.  * If we select r to be of the form:
  784.  *
  785.  *    r == Q*(2^j)*(3^k)*(z^2)        (Q == 1, j>=0, k>=0, z>0)
  786.  *
  787.  * then by use of {note 7}:
  788.  *
  789.  *    L(r, h*2^n-1) == L(Q*(2^j)*(3^k)*(z^2), h*2^n-1)
  790.  *              == L((2^j)*(3^k)*(z^2), h*2^n-1)
  791.  *              == 1                           {note 2}
  792.  *
  793.  * and thus, [condition 2] is met.
  794.  *
  795.  * If we select r to be of the form:
  796.  *
  797.  *    r == Q*(2^j)*(3^k)*(z^2)        (Q is prime>2, j>=0, k>=0, z>0)
  798.  *
  799.  * then by use of {note 7}:
  800.  *
  801.  *    L(r, h*2^n-1) == L(Q*(2^j)*(3^k)*(z^2), h*2^n-1)
  802.  *              == L(Q, h*2^n-1) * L((2^j)*(3^k)*(z^2), h*2^n-1) {A3.5}
  803.  *              == L(Q, h*2^n-1) * 1                   {note 2}
  804.  *              == L(h*2^n-1, Q) * (-1)^((h*2^n-2)*(Q-1)/4)      {A3.8}
  805.  *              == L(h*2^n-1 mod Q, Q)*(-1)^((h*2^n-2)*(Q-1)/4)  {note 1}
  806.  *              == J(h*2^n-1 mod Q, Q)*(-1)^((h*2^n-2)*(Q-1)/4)  {note 0}
  807.  *
  808.  * When does J(h*2^n-1 mod Q, Q)*(-1)^((h*2^n-2)*(Q-1)/4) take the value of 1,
  809.  * thus satisfy [condition 2]?  The answer depends on Q.  Now Q is a prime>2,
  810.  * thus Q mod 4 == 1 or -1.
  811.  *
  812.  * Take Q mod 4 == 1:
  813.  *
  814.  *    Q mod 4 == 1  implies  (-1)^((h*2^n-2)*(Q-1)/4) == 1
  815.  *
  816.  * Thus:
  817.  *
  818.  *    L(D, h*2^n-1) == L(h*2^n-1 mod Q, Q) * (-1)^((h*2^n-2)*(Q-1)/4)
  819.  *              == L(h*2^n-1 mod Q, Q)
  820.  *              == J(h*2^n-1 mod Q, Q)
  821.  *
  822.  * Take Q mod 4 == -1:
  823.  *
  824.  *    Q mod 4 == -1  implies  (-1)^((h*2^n-2)*(Q-1)/4) == -1
  825.  *
  826.  * Thus:
  827.  *
  828.  *    L(D, h*2^n-1) == L(h*2^n-1 mod Q, Q) * (-1)^((h*2^n-2)*(Q-1)/4)
  829.  *              == L(h*2^n-1 mod Q, Q) * -1
  830.  *              == -J(h*2^n-1 mod Q, Q)
  831.  *
  832.  * Therefore [condition 2] is met by selecting  D = Q*(2^j)*(3^k)*(z^2),
  833.  * where Q is prime>2, j>=0, k>=0, z>0; if and only if one of the following
  834.  * to cases are true:
  835.  *
  836.  *    Q mod 4 ==  1  and  J(h*2^n-1 mod Q, Q) == 1
  837.  *    Q mod 4 == -1  and  J(h*2^n-1 mod Q, Q) == -1
  838.  *
  839.  ***
  840.  *
  841.  * In conclusion, we can compute v(1) by attempting to do the following:
  842.  *
  843.  * h mod 3 != 0
  844.  *
  845.  *     we return:
  846.  *
  847.  *       v(1) == 4
  848.  *
  849.  * h mod 3 == 0
  850.  *
  851.  *     define:
  852.  *
  853.  *       r == abs(a^2 - b^2*D)
  854.  *       alpha == (a + b*sqrt(D))^2/r
  855.  *
  856.  *     we return:
  857.  *
  858.  *       v(1) = alpha^1 + alpha^(-1)
  859.  *
  860.  *     if and only if we can find a given a, b, D that obey all the
  861.  *     following selection rules:
  862.  *
  863.  *       D is square free
  864.  *
  865.  *       D == P*(2^f)*(3^g)        (P is prime>2, f,g == 0 or 1)
  866.  *
  867.  *       (a^2 - b^2*D)/r == -1
  868.  *
  869.  *       r == Q*(2^j)*(3^k)*(z^2)    (Q==1 or Q is prime>2, j>=0, k>=0, z>0)
  870.  *
  871.  *         one of the following is true:
  872.  *           P mod 4 ==  1  and  J(h*2^n-1 mod P, P) == -1
  873.  *           P mod 4 == -1  and  J(h*2^n-1 mod P, P) ==  1
  874.  *
  875.  *       if Q is prime, then one of the following is true:
  876.  *           Q mod 4 ==  1  and  J(h*2^n-1 mod Q, Q) == 1
  877.  *           Q mod 4 == -1  and  J(h*2^n-1 mod Q, Q) == -1
  878.  *
  879.  *     If we cannot find a v(1) quickly enough, then we will give up
  880.  *     testing h*2^n-1.  This does not happen too often, so this hack
  881.  *     is not too bad.
  882.  *
  883.  ***
  884.  *
  885.  * input:
  886.  *    h    h as in h*2^n-1
  887.  *    n    n as in h*2^n-1
  888.  *
  889.  * output:
  890.  *    returns v(1), or -1 is there is no quick way
  891.  */
  892. define
  893. gen_v1(h, n)
  894. {
  895.     local d;    /* the 'D' value to try */
  896.     local val_mod;    /* h*2^n-1 mod 'D' */
  897.     local i;
  898.  
  899.     /*
  900.      * check for case 1
  901.      */
  902.     if (h % 3 != 0) {
  903.         /* v(1) is easy to compute */
  904.         return 4;
  905.     }
  906.  
  907.     /*
  908.      * We will try all 'D' values until we find a proper v(1)
  909.      * or run out of 'D' values.
  910.      */
  911.     for (i=0; i < quickmax; ++i) {
  912.  
  913.         /* grab our 'D' value */
  914.         d = d_qval[i];
  915.  
  916.         /* compute h*2^n-1 mod 'D' quickly */
  917.         val_mod = (h*pmod(2,n%(d-1),d)-1) % d;
  918.  
  919.         /*
  920.          * if 'D' mod 4 == 1, then
  921.          *    (h*2^n-1) mod 'D' can not be a quadratic residue of 'D'
  922.          * else
  923.          *    (h*2^n-1) mod 'D' must be a quadratic residue of 'D'
  924.          */
  925.         if (d%4 == 1) {
  926.             /* D mod 4 == 1, so check for J(D, h*2^n-1) == -1 */
  927.             if (jacobi(val_mod, d) == -1) {
  928.                 /* it worked, return the related v(1) value */
  929.                 return v1_qval[i];
  930.             }
  931.         } else {
  932.             /* D mod 4 == -1, so check for J(D, h*2^n-1) == 1 */
  933.             if (jacobi(val_mod, d) == 1) {
  934.                 /* it worked, return the related v(1) value */
  935.                 return v1_qval[i];
  936.             }
  937.         }
  938.     }
  939.  
  940.     /*
  941.      * This is an example of a more complex proof construction.
  942.      * The code above will not be able to find the v(1) for:
  943.      *
  944.      *            81*2^81-1
  945.      *
  946.      * We will check with:
  947.      *
  948.      *    v(1)=81      D=6557      a=79      b=1      r=316
  949.      *
  950.      * Now, D==79*83 and r=79*2^2.  If we show that:
  951.      *
  952.      *    J(h*2^n-1 mod 79, 79) == -1
  953.      *    J(h*2^n-1 mod 83, 83) == 1
  954.      *
  955.      * then we will satisfy [condition 1].  Observe:
  956.      *
  957.       *    79 mod 4 == -1  implies  (-1)^((h*2^n-2)*(79-1)/4) == -1
  958.       *    83 mod 4 == -1  implies  (-1)^((h*2^n-2)*(83-1)/4) == -1
  959.      *
  960.      *    J(D, h*2^n-1) == J(83, h*2^n-1) * J(79, h*2^n-1)
  961.      *              == J(h*2^n-1, 83) * (-1)^((h*2^n-2)*(83-1)/4) *
  962.      *                 J(h*2^n-1, 79) * (-1)^((h*2^n-2)*(79-1)/4)
  963.      *              == J(h*2^n-1 mod 83, 83) * -1 *
  964.      *                 J(h*2^n-1 mod 79, 79) * -1
  965.      *              ==  1 * -1 *
  966.      *             -1 * -1
  967.      *              == -1
  968.      *
  969.      * We will also satisfy [condition 2].  Observe:
  970.      *
  971.      *    (a^2 - b^2*D)/r == (79^2 - 1^1*6557)/316
  972.      *            == -1
  973.      *
  974.      *    L(r, h*2^n-1) == L(Q*(2^j)*(3^k)*(z^2), h*2^n-1)
  975.      *              == L(79, h*2^n-1) * L(2^2, h*2^n-1)
  976.      *              == L(79, h*2^n-1) * 1
  977.      *              == L(h*2^n-1, 79) * (-1)^((h*2^n-2)*(79-1)/4)
  978.      *              == L(h*2^n-1, 79) * -1
  979.      *              == L(h*2^n-1 mod 79, 79) * -1
  980.      *              == J(h*2^n-1 mod 79, 79) * -1
  981.      *              == -1 * -1
  982.      *              == 1
  983.      */
  984.     if (jacobi( ((h*pmod(2,n%(79-1),79)-1)%79), 79 ) == -1 &&
  985.         jacobi( ((h*pmod(2,n%(83-1),83)-1)%83), 83 ) == 1) {
  986.         /* return the associated v(1)=81 */
  987.         return 81;
  988.     }
  989.  
  990.     /* no quick and dirty v(1), so return -1 */
  991.     return -1;
  992. }
  993.  
  994. /*
  995.  * ldebug - print a debug statement
  996.  *
  997.  * input:
  998.  *    funct    name of calling function
  999.  *    str    string to print
  1000.  */
  1001. define
  1002. ldebug(funct, str)
  1003. {
  1004.     if (lib_debug > 0) {
  1005.         print "DEBUG:", funct:":", str;
  1006.     }
  1007.     return;
  1008. }
  1009.  
  1010. global lib_debug;
  1011. if (lib_debug >= 0) {
  1012.     print "lucas(h, n) defined";
  1013. }
  1014.